package com.huawei.java.alogrithm;

import com.huawei.java.model.SiteNode;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: CodeCraft-2022
 * @description:
 * @author: hzy
 * @create: 2022-03-23 19:22
 **/
public class AllocatedCollection {
    private static Map<String, List<Integer>> maps = new HashMap<>();

    public static Integer getMaxListSize() {
        return maxListSize;
    }

    public static void setMaxListSize(Integer maxListSize) {
        AllocatedCollection.maxListSize = maxListSize;
    }

    private static Integer maxListSize;

    public static void init(List<SiteNode> siteNodes) {
        for (SiteNode node : siteNodes) {
            List initList = new ArrayList();
            for (int i = 0; i < maxListSize; i++) {
                initList.add(0);
            }
            maps.put(node.getName(), initList);
        }
    }

    public static List<Integer> getBySiteName(String name) {
        return maps.get(name);
    }

    public static Integer getValidSize(String name) {
        List<Integer> list = maps.get(name);
        int validSize = 0;
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i) > 0) {
                validSize++;
            }
        }
        return validSize;
    }

    public static void setBySiteName(String name, List<Integer> newList) {
        maps.remove(name);
        Collections.sort(newList);
        if (newList.size() > maxListSize) {
            newList.remove(0);
        }

        maps.put(name, newList);
    }

    public static int computeCost(String name, Integer cost) {
        List<Integer> list = maps.get(name);
        int validSize = getValidSize(name);
        if (validSize < maxListSize - 1) {
            return 0;
        }

        if (validSize == maxListSize - 1) {
            List<Integer> tmpList = new ArrayList<>(list);
            tmpList.add(cost);
            Collections.sort(tmpList);
            tmpList.remove(0);
            return tmpList.get(0);
        }

        if (validSize > maxListSize - 1) {
            int oldcost = list.get(0);
            if (cost <= oldcost) {
                return 0;
            }

            List<Integer> tmpList = new ArrayList<>(list);
            tmpList.add(cost);
            Collections.sort(tmpList);
            return tmpList.get(1) - tmpList.get(0);
        }
        return 0;
    }

    public static void addNewCost(String name, Integer cost) {
        List<Integer> list = maps.get(name);
        list.add(cost);
        Collections.sort(list);
        list.remove(0);
    }

    public static Integer maxCanAddCost(String name, Integer beforeCost) {
        List<Integer> list = maps.get(name);
        int validSize = getValidSize(name);
        if (validSize < maxListSize - 1) {
            return Integer.MAX_VALUE;
        }

        if (validSize == maxListSize - 1) {
            return 0;
        }

        if (list.get(0) - beforeCost <= 0) {
            return 0;
        }

        return list.get(0) - beforeCost;
    }

    public static int getCost(String name) {
        return maps.get(name).get(0);
    }

    public static int insertPos(String name, int cost) {
        List<Integer> list = maps.get(name);
        int pos = -1;
        for (int i =0 ; i< list.size() - 1; i++) {
            if (list.get(i) < cost && cost <= list.get(i + 1)) {
                pos = i;
                break;
            }
        }
        return pos;
    }

    public static Integer chazhi(String name, int cost) {
        List<Integer> list = maps.get(name);
        Integer cha = Integer.MAX_VALUE;
        if (list.size() == 0) {
            return 0;
        }
        for (int i =0 ; i< list.size() - 1; i++) {
            if (list.get(i) < cost && cost <= list.get(i + 1)) {
                cha = list.get(i + 1) - cost;
                break;
            }
        }
        return cha;
    }
}
